﻿using System.Collections.Generic;
using System.Linq;
using LeanBoard.Models;
using LeanBoard.ServiceReference1;

namespace LeanBoard.Controllers
{
    class DominoChangeRequestsProvider : IProvideChangeRequests
    {
        public ItemsCollection GetItemsForTheLeanBoard()
        {
            eMgrWSP eMgrWSP = new eMgrWSPClient();
            var getdocumentsRequest = new GETDOCUMENTSRequest();
            getdocumentsRequest.STRRELEASE = "20100821";
            getdocumentsRequest.STRTYPE = string.Empty;
            getdocumentsRequest.STRTEAM = string.Empty;
            GETDOCUMENTSResponse getdocumentsResponse = eMgrWSP.GETDOCUMENTS(getdocumentsRequest);

            WSPDOC[] document = getdocumentsResponse.GETDOCUMENTSReturn.DOCUMENT;

            //WSPDOC[] document = new WSPDOC[0];

            var itemsCollection = new ItemsCollection();

            Dictionary<string, IList<Item>> items = new Dictionary<string, IList<Item>>();

            foreach (WSPDOC wspdoc in document)
            {
                Item item = new Item()
                                {
                                    DevResponsible = wspdoc.TASKRESPONSIBLE,
                                    Id = wspdoc.NRDOC,
                                    QaResponsible = wspdoc.TASKRESPONSIBLE,
                                    Status = wspdoc.LEANSTATUS,
                                    Title = wspdoc.TITLE,
                                    Type = wspdoc.GLOBALTYPE,
                                    Unid = wspdoc.DOCID,
                                    Priority = wspdoc.PRIORITY,
                                    Team = wspdoc.TEAM,
                                    ItemType = wspdoc.DOCTYPE
                                };

                if (!items.ContainsKey(item.Status))
                    items.Add(item.Status, new List<Item>());

                items[item.Status].Add(item);
            }

            itemsCollection.Scheduled = items.ContainsKey(DominoStatuses.ScheduledStatusKey) ? items[DominoStatuses.ScheduledStatusKey] : new List<Item>();
            itemsCollection.Analysis = items.ContainsKey(DominoStatuses.AnalysisStatusKey) ? items[DominoStatuses.AnalysisStatusKey] : new List<Item>();
            itemsCollection.AnalysisReadyForReview = items.ContainsKey(DominoStatuses.ReadyForAnalysisReviewStatusKey) ? items[DominoStatuses.ReadyForAnalysisReviewStatusKey] : new List<Item>();
            itemsCollection.AnalysisReview = items.ContainsKey(DominoStatuses.AnalysisReview) ? items[DominoStatuses.AnalysisReview] : new List<Item>();
            itemsCollection.ReadyForDevelopment = items.ContainsKey(DominoStatuses.ReadyForDevelopment) ? items[DominoStatuses.ReadyForDevelopment] : new List<Item>();
            itemsCollection.Development = items.ContainsKey(DominoStatuses.DevelopmentStatusKey) ? items[DominoStatuses.DevelopmentStatusKey] : new List<Item>();
            itemsCollection.ReadyForCodeReview = items.ContainsKey(DominoStatuses.DevelopmentDoneStatusKey) ? items[DominoStatuses.DevelopmentDoneStatusKey] : new List<Item>();
            itemsCollection.CodeReview = items.ContainsKey(DominoStatuses.ReviewStatusKey) ? items[DominoStatuses.ReviewStatusKey] : new List<Item>();
            itemsCollection.ReadyForApporval = items.ContainsKey(DominoStatuses.ReviewDoneStatusKey) ? items[DominoStatuses.ReviewDoneStatusKey] : new List<Item>();
            itemsCollection.Testing = items.ContainsKey(DominoStatuses.TestingStatusKey) ? items[DominoStatuses.TestingStatusKey] : new List<Item>();
            itemsCollection.Done = items.ContainsKey(DominoStatuses.DoneStatusKey) ? items[DominoStatuses.DoneStatusKey] : new List<Item>();

            itemsCollection.Scheduled = itemsCollection.Scheduled.OrderBy(x => x.Priority).ToArray();
            itemsCollection.ReadyForDevelopment = itemsCollection.ReadyForDevelopment.OrderBy(x => x.Priority).ToArray();

            return itemsCollection;
        }
    }
}